package Logika;
import bluetooth.Client;
import java.util.*;



/**
 *
 * @author mejcu
 */

// Will be used later to synchronized the server with clients

public class Synchronizer implements Runnable
{
    private int tick;
    private Client client;
    private int lastToken;
    private boolean isPolling;
    private long lastPollTime;
    private int[]   randomDecision	        ={Zmienne.TURN_NORTH,Zmienne.TURN_EAST,Zmienne.TURN_SOUTH,Zmienne.TURN_WEST};
    private Random random                   = new Random();
    private Hashtable table=new Hashtable();


    public Synchronizer(Client _client)
    {

        this.client=_client;
        lastToken=0;
        lastPollTime=0;
        isPolling=true;

    }

    public void start()
    {
        isPolling=true;
    }

    public void stop()
    {
        isPolling=false;
    }

    public int getTick()
    {
        return tick;
    }

    public void run()
    {
        long diff;
        while(isPolling)
        {
            diff=System.currentTimeMillis()-lastPollTime;
            if(diff>Zmienne.INTERVAL_POLL)
            {
                String komunikat="";
                lastPollTime=System.currentTimeMillis();
                komunikat+=(char)randomDecision[random.nextInt(5)];
                client.sendData(komunikat);
            }
            else
            {
                try
                {
                    Thread.sleep(Zmienne.INTERVAL_POLL-diff);
                }
                catch(InterruptedException e)
                {
                    System.err.println("I was unable to sleep so long...");
                }
            }
        }
    }

}
